package com.jty.module_login.ui.activity


import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.lib_base.config.AppConstants
import com.gyf.immersionbar.ImmersionBar
import com.jty.lib_base.base.BaseActivity
import com.jty.lib_base.route.RouteCenter
import com.jty.module_login.BR
import com.jty.module_login.R
import com.jty.module_login.databinding.LoginActivitySplashBinding
import com.jty.module_login.viewmodel.SplashViewModel
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit


/**
 * author: zhouyh
 * created on: 2021/5/12 6:58 下午
 * description:
 */
@Route(path=AppConstants.Router.App.F_Splash)
class SplashActivity : BaseActivity<LoginActivitySplashBinding, SplashViewModel>() {

    override fun initContentView(): Int {
       return R.layout.login_activity_splash
    }

    override fun initParam() {
        ImmersionBar.hideStatusBar(window)
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    override fun initData() {
        AppConstants.Path.FILE_PATH = filesDir.absolutePath
        toLogin()
        //这里进行位置权限申请，会造成部分机型无法正常启动，所以不在此处进行权限申请
//        PermissionUtil.reqLocation(activity = this, callback = { allGranted, _, _->
//            if (allGranted){
//                toLogin()
//            } else {
//                finish()
//            }
//        })

        /*LivePermissions(this).request(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CAMERA
        ).observe(this,  {
            var isToLogin = true
            when (it) {
                is PermissionResult.Grant -> {  //权限允许
                    //Toast.makeText(this, "Grant", Toast.LENGTH_SHORT).show()
                }
                is PermissionResult.Rationale -> {  //权限拒绝
                    it.permissions.forEach {s->
                        Logger.e("permission", "Rationale:${s}")//被拒绝的权限
                    }
                    isToLogin = false
                    //Toast.makeText(this, "Rationale", Toast.LENGTH_SHORT)show()
                }
                is PermissionResult.Deny -> {   //权限拒绝，且勾选了不再询问
                    it.permissions.forEach {s->
                        Logger.e("permission", "deny:${s}")//被拒绝的权限
                    }
                    isToLogin = false
                    //Toast.makeText(this, "deny", Toast.LENGTH_SHORT).show()
                }
            }
            if(isToLogin) {
                toLogin()
            } else {
                finish()
            }
        })*/
    }



    private fun toLogin(){
        viewModel.addSubscribe(
            Flowable.timer(1500,TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe{
                    if (!viewModel.model.isLogin()){
                        startContainerActivity(AppConstants.Router.Login.F_LOGIN)
                        overridePendingTransition(R.anim.h_fragment_enter,0)
                    }else{
                        RouteCenter.navigate(AppConstants.Router.Main.A_MAIN)
                    }
                    finish()
                }
        )
    }
}